package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.FindJobRequestEntity;
import com.sgb.kjwl.model.entity.MaterialFilterEntity;
import com.sgb.kjwl.view.ui.adapter.material.MaterialFilterAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class LookingforAJobFliterPopWindow extends BasePopWindow {
    private TextView areaView;
    private TextView typeView;
    private TextView levelView;
    private TextView recordView;
    private TextView okView;
    private TextView resetView;

    private MaxRecyclerView areaRecyclerList;
    private MaxRecyclerView levelList;
    private MaxRecyclerView typeList;
    private MaxRecyclerView recordList;

    private MaterialFilterAdapter typeAdapter;
    private MaterialFilterAdapter levelAdapter;
    private MaterialFilterAdapter recordAdapter;
    private MaterialFilterAdapter areaAdapter;

    private LookingforAJobFliterPopWindow.FilterResultClick filterResultClick;

    private FindJobRequestEntity filterJson;

    private int typePosition = -1;

    public interface FilterResultClick {
        void result(FindJobRequestEntity filterJson);
    }

    public void setFilterResultClick(LookingforAJobFliterPopWindow.FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public LookingforAJobFliterPopWindow(Context context) {
        super(context, DIR_RIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwin_power_fliter;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);

        typeView = holder.getPop().findViewById(R.id.type_view);
        levelView = holder.getPop().findViewById(R.id.level_view);
        recordView = holder.getPop().findViewById(R.id.record_view);
        areaView = holder.getPop().findViewById(R.id.area_view);

        typeList = holder.getPop().findViewById(R.id.type_list);
        levelList = holder.getPop().findViewById(R.id.level_list);
        recordList = holder.getPop().findViewById(R.id.record_list);
        areaRecyclerList = holder.getPop().findViewById(R.id.area_list);

        typeList.setLayoutManager(new GridLayoutManager(mContext, 3));
        levelList.setLayoutManager(new GridLayoutManager(mContext, 3));
        recordList.setLayoutManager(new GridLayoutManager(mContext, 3));
        areaRecyclerList.setLayoutManager(new GridLayoutManager(mContext, 3));

        //资质类型
        typeAdapter = new MaterialFilterAdapter(mContext, null);
        typeAdapter.setItemSelect(true);
        typeAdapter.setDefItem(-1);
        typeAdapter.setOnItemClick(new AdapterOnItemClick<MaterialFilterEntity>() {


            @Override
            public void onItemClick(MaterialFilterEntity materialFilterEntity, int position) {
                if (typePosition != position) {
                    typePosition = position;
                    filterJson.setFirstCategoryNo(materialFilterEntity.getId());
                    levelView.setVisibility(View.VISIBLE);
                    levelList.setVisibility(View.VISIBLE);
                    levelAdapter.setDatas(materialFilterEntity.getDatas());
                    levelAdapter.setDefItem(-1);
                    filterJson.setPayfor("");

                }
            }
        });
        typeList.setAdapter(typeAdapter);

        //资质等级
        levelAdapter = new MaterialFilterAdapter(mContext, null);
        levelAdapter.setItemSelect(true);
        levelAdapter.setDefItem(-1);
        levelAdapter.setOnItemClick(new AdapterOnItemClick<MaterialFilterEntity>() {
            @Override
            public void onItemClick(MaterialFilterEntity materialFilterEntity, int position) {
                filterJson.setAreacode(materialFilterEntity.getId());
            }
        });
        levelList.setAdapter(levelAdapter);

        //备案单位
        recordAdapter = new MaterialFilterAdapter(mContext, null);
        recordAdapter.setItemSelect(true);
        recordAdapter.setDefItem(-1);
        recordAdapter.setOnItemClick(new AdapterOnItemClick<MaterialFilterEntity>() {
            @Override
            public void onItemClick(MaterialFilterEntity materialFilterEntity, int position) {
                filterJson.setAreacode(materialFilterEntity.getId());

            }
        });
        recordList.setAdapter(recordAdapter);

        //区域，省内、省外
        areaAdapter = new MaterialFilterAdapter(mContext, null);
        areaAdapter.setItemSelect(true);
        areaAdapter.setDefItem(0);
        areaAdapter.setOnItemClick(new AdapterOnItemClick<MaterialFilterEntity>() {
            @Override
            public void onItemClick(MaterialFilterEntity materialFilterEntity, int position) {
                filterJson.setAreacode(materialFilterEntity.getId());
            }
        });
        areaRecyclerList.setAdapter(areaAdapter);

        filterJson = new FindJobRequestEntity();

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (filterResultClick != null) {
                    filterResultClick.result(filterJson);
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetData();
            }
        });
    }

    private void resetData() {

        typeAdapter.setDefItem(-1);
        typePosition = -1;


        levelView.setVisibility(View.GONE);
        levelList.setVisibility(View.GONE);
        levelAdapter.setDefItem(-1);


        recordAdapter.setDefItem(-1);


        areaAdapter.setDefItem(0);


    }

    public void setTypeLevelData(List<MaterialFilterEntity> data) {
        if (data.size() == 0) {
            typeView.setVisibility(View.GONE);
        } else {
            typeView.setVisibility(View.VISIBLE);
        }
        typeAdapter.setDatas(data);

    }

    public void setRecordData(List<MaterialFilterEntity> data) {
        if (data.size() == 0) {
            recordView.setVisibility(View.GONE);
        } else {
            recordView.setVisibility(View.VISIBLE);
        }
        recordAdapter.setDatas(data);
    }

    public void setAreaData(List<MaterialFilterEntity> data) {
        if (data.size() == 0) {
            areaView.setVisibility(View.GONE);
        } else {
            areaView.setVisibility(View.VISIBLE);
        }
        areaAdapter.setDatas(data);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}
